<template>
  <el-row :gutter="25">
   <el-col :span="leftSpan">
     <el-table :data="value">
        <el-table-column v-for="(colunm,index) in tableColunms" :prop="colunm.vModel" :label="colunm.label"></el-table-column>
     </el-table>
   </el-col>
    <el-col :span="24-leftSpan">
      <el-form :model="form" label-width="120px">
        <el-row v-for="(colunm,index) in tableColunms" style="margin-bottom: 20px">
          <custom-form-item  :element="colunm" v-model="form[colunm.vModel]" ></custom-form-item>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item>
              <el-button type="primary" size="mini" @click="addHandler">添加</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-col>
  </el-row>

</template>

<script type="text/jsx">
import CustomFormItem from "@/components/CustomFormItem";
export default {
  name: "TableForm",
  components:{
    CustomFormItem
  },
  props:{
    value: {
      type: Array,
      default(){
        return []
      }
    },
    tableColunms: Array,
    leftSpan:Number
  },
  data() {
    return {
      list:[],
      form:{}
    }
  },
  methods:{
    addHandler() {
      let copy = this._.cloneDeep(this.form)
      this.list.push(copy)
      console.log(this.list)
      console.log(this.form)
    }
  },
  watch:{
    value:{
      handler(list) {
        this.list = list
      },
      immediate:true
    },
    list:{
      handler(nev,oldv) {
        this.$emit('input',nev)
      },
      deep:true
    },
    tableColunms:{
      handler(nv) {
        nv.forEach(element => {
          this.$set(this.form,element.vModel,element.default)
        })
      },
      immediate:true
    }
  },
}
</script>

<style scoped>

</style>
